<template>
  <div class="users">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form>
      <el-form-item style="margin-top: 22px;">
        <el-input
          style="width:350px"
          placeholder="请输入内容"
          clearable
          @keyup.enter.native="getUsers"
          v-model="searchCon"
        >
          <template slot="append">
            <el-button slot="append" icon="el-icon-search" @click="getUsers"></el-button>
          </template>
        </el-input>
        <el-button type="primary" @click="addUser">添加用户</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="usersTabel" border style="width: 100%;margin-bottom: 15px;">
      <template slot="empty">暂无数据</template>
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column prop="username" label="姓名" width="120"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="160"></el-table-column>
      <el-table-column prop="mobile" label="电话"></el-table-column>
      <el-table-column prop="create_time" label="创建日期">
        <template slot-scope="scope">{{scope.row.create_time | YYYYMMDD}}</template>
      </el-table-column>
      <el-table-column prop="mg_state" label="用户状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.mg_state" @change="changeStatus(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="delUser(scope.row.id)"
          ></el-button>
          <el-button
            type="success"
            icon="el-icon-check"
            circle
            size="mini"
            @click="showRightsList(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pagenum"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="currentChange"
    ></el-pagination>
    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="600px">
      <el-form :model="userForm" :rules="rules" :label-position="labelPosition">
        <el-form-item label="用户名" :label-width="labelWidth" prop="username">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="labelWidth" prop="password">
          <el-input v-model="userForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="labelWidth" prop="email">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="labelWidth" prop="mobile">
          <el-input v-model="userForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUserSub">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  data() {
    return {
      searchCon: "",
      usersTabel: [],
      pagenum: 1,
      pagesize: 2,
      total: 0,
      dialogVisible: false,
      labelPosition: "right",
      labelWidth: "120px",
      userForm: {
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ],
        email: [
          
        ],
        mobile: [
          
        ]
      }
    };
  },
  watch: {
    pagesize() {
      this.getUsers();
    },
    pagenum() {
      this.getUsers();
    }
  },
  methods: {
    //获取用户 列表
    getUsers() {
      this.$http("users", {}, "get", {
        query: this.searchCon,
        pagenum: this.pagenum,
        pagesize: this.pagesize
      }).then(res => {
        this.usersTabel = res.data.users;
        this.total = res.data.total;
      });
    },
    //每页条数改变触发
    sizeChange(val) {
      this.pagesize = val;
      this.pagenum = 1;
    },
    //当前页数变化
    currentChange(val) {
      this.pagenum = val;
    },
    //添加用户弹窗
    addUser() {
      this.dialogVisible = true;
    },
    //添加用户确认提交
    addUserSub(){
      this.$http('users',{
        ...this.userForm
      }, 'post').then(res => {
        this.dialogVisible = false
        this.$message({
          message: '添加用户成功',
          type: 'success'
        })
        this.getUsers()
      })
    },
    changeStatus(val) {
      console.log(val)
      this.$http(`users/${val.id}/state/${val.mg_state}`, {}, 'put')
      .then(res => {
        this.getUsers()
      })
    }
  },
  created() {
    this.getUsers();
  }
};
</script>

<style scoped lang="scss">
.users {
  background: #fff;
  border-radius: 5px;
  padding: 15px;
}
</style>
